<template>
  <!--表格查询通用模板-->
</template>

<script>

export default {
  name: "tableMixins",
  data() {
    return {
      //avue对象的ref值
      ref:"crud",
      // 搜索条件
      searchForm: {},
      // 表格加载标志
      tableLoading: false,
      // 表格数据
      tableData: [],
      // 表单数据
      form: {},
      //当前选中
      obj: {},
      // 分页参数
      page: {
        pageSize: 10,
        currentPage:1,
        total:''
      },
      //新增url
      addUrl:"insertOrUpdate",
      //删除url
      delUrl:"delete-by-id",
      //修改
      updateUrl:'insertOrUpdate',
      //查询url
      listUrl:'findPage',
      search:{}
    }
  },
  created() {

  },
  methods: {
    /**
     * 获取id字段
     **/
    getIdField() {
      // console.log('444444')
      let field = "id";
      this.tableOption.column.filter(x => {
        if (x.label == 'ID') {
          field = x.prop;
        }
      })
      return field;
    },



    /**
     * 新增保存
     * @param row 行数据
     * @param done 完成回调
     * @param loading 加载回调
     */
    handleSave: function (row, done, loading) {
      this.$httpService.post("/" + this.url + "/"+this.addUrl, row).then(res => {
        this.$message.success('新增成功');
        this.getList();
        done();
        loading();
      }).catch(res=>{
        loading();
      })
    },


    /**
     * 修改保存
     * @param row 行数据
     * @param index 行索引
     * @param done 完成回调toggleSelection
     * @param loading 加载回调
     */
    handleUpdate: function (row, index, done, loading) {
      this.$httpService.post("/" + this.url + "/"+this.updateUrl, row).then(res => {
        this.$message.success('修改成功');
        this.getList();
        done();
        loading();
      }).catch(res=>{
        loading();
      })
    },

    /**
     * 删除
     * @param row 行数据
     * @param index 行索引
     */
    rowDel: function (row) {
      // console.log('删除')
      this.$confirm('是否确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$httpService.del("/" + this.url + "/"+this.delUrl+"/" + row[this.getIdField()]).then(res => {
          this.$message.success('删除成功');
          this.getList();
        })
      })
    },

    /**
     * 获取列表数据
     */
    getList() {
      this.tableLoading = true;
      let query = {}
      for (let i in this.searchForm) {
        if (  typeof (this.searchForm[i]) !== "undefined" && this.searchForm[i].length == 0  ){
           delete  this.searchForm[i]
        }
      }
      query = Object.assign({}, this.searchForm, this.page,this.search);
      query.current = this.page.currentPage
      query.size = this.page.pageSize
      if((this.searchForm.createTime && this.searchForm.createTime.length==2)){
        query.startTime = this.searchForm.createTime[0]
        query.endTime = this.searchForm.createTime[1]
      }
      if((this.searchForm.changeTime && this.searchForm.changeTime.length==2)){
        query.startTime = this.searchForm.changeTime[0]
        query.endTime = this.searchForm.changeTime[1]
      }
      // console.log('222222',query)
      this.$httpService.post("/" + this.url + "/"+this.listUrl,query).then(res => {
        this.tableData = res.records
        this.page.total = res.total;
        this.tableLoading = false
      })
    },

    /**
     * 分页大小改变
     * @param pageSize 分页大小
     */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      // this.getList();
    },
    /**
     * 当前页改变
     * @param current 当前页
     */
    currentChange(current) {
      this.page.currentPage = current;
      // this.getList();
    },
    /**
     * 搜索
     * @param form 查询条件
     * @param done 完成回调
     */
    searchChange(form, done) {
      this.searchForm = form;
      this.page.pageCount = 1;
      this.getList();
      done();
    },

    /**
     * 刷新
     */
    refreshChange() {
      this.searchForm=this.$options.data().searchForm;
      this.form={};
      this.page.pageSize = 10;
      this.page.currentPage = 1;
      this.getList();
    },
    //打开修改弹框
    rowCell(row, index){
      this.$refs[this.ref].rowEdit(row, index)
    },
    //清空
    searchReset(){
    }
  }
}
</script>

<style scoped>

</style>
